# coding=utf-8
import os
import qianfan


def gpt(question):
    # 读出公钥密钥
    with open("QIANFAN_ACCESS_KEY", 'r', encoding='utf-8') as f:
        QIANFAN_ACCESS_KEY = f.read()
    with open("QIANFAN_SECRET_KEY", "r", encoding="utf-8") as f:
        QIANFAN_SECRET_KEY = f.read()

    # 官方文档
    os.environ["QIANFAN_ACCESS_KEY"] = QIANFAN_ACCESS_KEY
    os.environ["QIANFAN_SECRET_KEY"] = QIANFAN_SECRET_KEY

    # 创建Chat robot
    chat_robot = qianfan.ChatCompletion()

    # 传入字典，得到响应，完成对话
    resp = chat_robot.do(
        messages=[{
            "role": "user",
            "content": question
        }]
    )

    # print(resp)
    # print(resp.body)

    # 返回字典中索引result的内容
    return resp.body['result']

# 测试
# print(gpt('你好'))
